package com.study.lxy.springcloud.brac_ia.interceptor;

import com.study.lxy.springcloud.brac_ia.entity.ClientUser;
import com.study.lxy.springcloud.common.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisUtils redisUtils;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie:cookies){
            if("SESSION_ID".equals(cookie.getName())){
                ClientUser clientUser = (ClientUser) redisUtils.get("SESSION_ID:" + cookie.getValue());
                if(clientUser != null){
                    return true;
                }
            }
        }
        return false;
    }

}
